Skip to content

Conversation

@ToshikiNakamura0412
Copy link
Contributor

No description provided.


変数に値が格納されているかどうかを調べることができるクラス

ROSなどのコールバック関数で値が格納されているかどうかを確認する際にも利用できる
Copy link
Contributor

@Taka-Kazu Taka-Kazu May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

どういうユースケースなのかよくわからないです。ROSのサブスクライバのコールバック関数は値をポインタで渡してくるので、単にそれがnullptrかどうかを確認すれば良いと思います。また、コールバック関数が呼ばれたときに引数がnullptrでない保証があるかどうかは把握していないですが、普通わざわざ確認しないと思います

{
std::cout << "値が含まれていません。" << std::endl;
}
// 値が含まれていないので値の取得もできない
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

値が無い(無効である)ことを示せるのはそうですが、例えばint型を返す関数で、「返り値が0未満ならエラーを表している」という実装をする代わりに、「エラーの場合は無効値(std::nullopt)を返す。値が入っていればそれは有効である」という実装ができ、その関数を呼び出す側からすると値ではなく型で有効/無効の判別ができる、というのがoptionalの代表的な利点であると思うので、そういうケースについても示せると良いと思います

Copy link
Contributor

@Taka-Kazu Taka-Kazu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一旦マージする

@Taka-Kazu Taka-Kazu merged commit 6e4e55e into master Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants